package com.cms.controller.admin;

import com.cms.domain.Feedback;
import com.cms.entity.Company;
import com.cms.entity.Site;
import com.cms.entity.Web;
import com.cms.service.CompanyService;
import com.cms.service.SiteService;
import com.cms.service.WebService;
import com.cms.utils.PinYinUtils;
import com.cms.utils.TemplateUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;


/**
 * Controller - 站点
 * 
 * 
 * 
 */
@Controller("adminSiteController")
@RequestMapping("/admin/site")
public class SiteController extends BaseController {

	@Autowired
	private SiteService siteService;
	@Autowired
	private WebService webService;
	@Autowired
	private CompanyService companyService;

	/**
	 * 列表
	 */
	@RequestMapping
	public String index(Integer pageNumber, ModelMap modelMap) {
		setListQuery();
		if(pageNumber==null){
			pageNumber = 1;
		}
		modelMap.addAttribute("page", siteService.findPage(pageNumber,PAGE_SIZE));
		return getView("site/index");
	}

	/**
	 * 添加
	 */
	@RequestMapping("/add")
	public String add(ModelMap modelMap) {
		modelMap.addAttribute("templates", TemplateUtils.getTemplates());
	    return getView("site/add");
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public String save(Site site) {
		if(StringUtils.isBlank(site.getPcTemplate())){
			site.setPcTemplate(PinYinUtils.convertLower(site.getName()));
		}
		if(site.getIsMobile() && StringUtils.isBlank(site.getMobileTemplate())){
			site.setMobileTemplate(PinYinUtils.convertLower(site.getName())+"_mobile");
		}
		site.setCreateDate(LocalDateTime.now());
		site.setUpdateDate(LocalDateTime.now());
		siteService.save(site);
		//网站信息
		Web web = new Web();
		web.setSiteId(site.getId());
		webService.save(web);
		//公司信息
		Company company = new Company();
		company.setSiteId(site.getId());
		companyService.save(company);
		return "redirect:"+getListQuery("/admin/site");
	}

	/**
	 * 编辑
	 */
	@RequestMapping("/edit")
	public String edit(Integer id,ModelMap modelMap) {
		modelMap.addAttribute("site", siteService.getById(id));
		modelMap.addAttribute("templates", TemplateUtils.getTemplates());
		return getView("site/edit");
	}

	/**
	 * 更新
	 */
	@RequestMapping("/update")
	public String update(Site site) {
		site.setUpdateDate(LocalDateTime.now());
		siteService.updateById(site);
		return "redirect:"+getListQuery("/admin/site");
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Feedback delete(Integer[] ids) {
		if(ArrayUtils.isNotEmpty(ids)){
			webService.deleteBySiteId(Arrays.asList(ids));
			companyService.deleteBySiteId(Arrays.asList(ids));
			siteService.removeByIds(Arrays.asList(ids));
		}
		return Feedback.success(new HashMap<>());
	}

	/**
	 * 设置为默认站点
	 */
	@RequestMapping("/setDefault")
	public String setDefault(Integer id){
		Site defaultSite = siteService.findDefault();
		defaultSite.setIsDefault(false);
		if(StringUtils.isNotBlank(defaultSite.getPcTemplate())){
			defaultSite.setCat(defaultSite.getPcTemplate());
		}else if(StringUtils.isNotBlank(defaultSite.getMobileTemplate())){
			defaultSite.setCat(defaultSite.getMobileTemplate());
		}
		defaultSite.setUpdateDate(LocalDateTime.now());
		siteService.updateById(defaultSite);
		Site site = siteService.getById(id);
		site.setIsDefault(true);
		site.setUpdateDate(LocalDateTime.now());
		site.setCat("");
		siteService.updateById(site);
		return "redirect:"+getListQuery("/admin/site");
	}

}